Solved A Curl like POST using NetworkHttpHandlerRef?

Is it possible to do a Curl style post using NetworkHttpHandlerRef?

I see that it has HandlePost, but so far haven't been successful in getting it working.

About to compile libCurl with OpenSSL support, but thought I would check here in case I don't need to.

Thanks!
Kent

hello,

you want to send file or just string data ?

did you had a look at this manual ?

Cheers,
Manuel

MAXON SDK Specialist

MAXON Registered Developer

Hi Manuel,

I want to send a HTTP post request with a couple of parameters, which will then return some JSON data.

I can’t see how to do this from the link you sent. I could open a tcp connection myself to port 80 of my URL, construct and build my own HTTPS header, send it and listen for the returned data.

The HandlePost function of the NetworkHttpHandlerRef class looks like it was written to do exactly this. But there is no documentation showing how to use it and no examples.

https://developers.maxon.net/docs/Cinema4DCPPSDK/html/classmaxon_1_1_network_http_handler_interface.html

I have filled in the DataDictionary with my parameters but when I call HandlePost it always seems to think there is an error in the MAXON result, but it contains no error message telling me what went wrong.

I would like to call something like this

curl https://api.myapi.com/v1 -d "product=myproduct" -X POST

Kent

This post is deleted!

hello,

This should work. memReq will have the returned json file in Ascii.
After asking the techTeam.

#include "maxon/network_url.h"  // for HTTP_POSTMETHOD and HTTP_POSTDATA
#include "maxon/file_utilities.h"

        iferr_scope;
	maxon::Url theServer{"http://localhost:8080"_s};

	maxon::String postData = "foo=bar&bin=go"_s;

	theServer.Set(maxon::URLFLAGS::HTTP_POSTMETHOD, maxon::HTTPMETHOD::POST) iferr_return;
	theServer.Set(maxon::URLFLAGS::HTTP_POSTDATA, maxon::CString(postData, maxon::StringEncodings::Utf8())) iferr_return;

	maxon::BaseArray<maxon::Char> memReq;
	iferr(maxon::FileUtilities::ReadFileToMemory(theServer, memReq))
		DiagnosticOutput("@", err);

	ApplicationOutput("answer @", memReq);
	
	maxon::String result(memReq);

	ApplicationOutput("result @", result);
	return maxon::OK;

the simple web server i used this with :

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import SocketServer
import json
import cgi

class Server(BaseHTTPRequestHandler):
    def _set_headers(self):
        self.send_response(200)
        #self.send_header('Content-type', 'application/json')
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        
    def do_HEAD(self):
        self._set_headers()
        
    # GET sends back a Hello world message
    def do_GET(self):
        self._set_headers()
        self.wfile.write(json.dumps({'hello': 'world', 'received': 'ok'}))
        
    # POST echoes the message adding a JSON field
    def do_POST(self):
        print 'do post'
        ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
        form = cgi.FieldStorage(
            fp=self.rfile,
            headers=self.headers,
            environ={'REQUEST_METHOD': 'POST'}
        )

        print form.getvalue("foo")
        print form.getvalue("bin")
        

        # refuse to receive non-json content
        if ctype != 'application/x-www-form-urlencoded':
            self.send_response(400)
            self.end_headers()
            print "Can't receive non application/x-www-form-urlencoded content", ctype
            return


       
        
        # send the message back
        self._set_headers()
        self.wfile.write(json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]))
        
def run(server_class=HTTPServer, handler_class=Server, port=8080):
    server_address = ('', port)
    httpd = server_class(server_address, handler_class)
    
    print 'Starting httpd on port %d...' % port
    httpd.serve_forever()
    
if __name__ == "__main__":
    run()

Cheers,
Manuel

MAXON SDK Specialist

MAXON Registered Developer

Hi Manuel,

Thanks for the example. That is excellent. For now though I have moved onto using libcurl, but when I come back to this again I will be sure to use this example again. I hope this gets added to the documentation somwhere.

Cheers,
Kent

hello,

don't worry, it will.

ps : I don't forget the OAuth example neither.

cheers,
Manuel

MAXON SDK Specialist

MAXON Registered Developer

hello,

i'll be set this thread as resolved tomorrow if nothing to add.

Cheers
Manuel

MAXON SDK Specialist

MAXON Registered Developer