10 years ago.

Help for use Post Method

hello, I want to use your library with the POST method to call a remote rpc server, can you make an example the body of the request is as follows: "{\" method \ ": \" MyMethod \ ", \" params \ ": [0] \" id \ ": 1}", thank you in advance for your help I am noob

Question relating to:

A http client sample program. http, HTTPClient, libMiMic, tcpip

1 Answer

10 years ago.

Hello,

HTTP post is maybe this example. I have not check this sample completely.

int main()
{
    net=new Net();//Net constructor must be created after started RTOS

    // manual setting
    cfg.setIpAddr(192,168,128,39);
    cfg.setNetMask(255,255,255,0);
    cfg.setGateway(192,168,128,254);    
    cfg.setSrvUPnP(false);
    cfg.setSrvMdns(false);

    // Create http client.
    // Socket must create between "net.start" with "new Net()"
    HttpClient http;
    
    //Start network
    net->start(cfg);

    if(http.connect(IpAddr(192,168,128,254),80)){
        if(http.sendMethod(HttpClient::HTTP_POST,"/mimic/")){
            const char* DATA="{json}"
            if(http.write(DATA,strlen(DATA))){
                if(http.getStatus()==200){
                    char* buf[256];
                    short len;
                    if(http.read(buf,256,&len)){
                        printf("%.*s",len,buf);
                    }
                }
            }
        }
        http.close();
    }    
    for(int c=0;;c=(c+1)%2){
        led4=c;
        Thread::wait(500);
    }
    return 0;
}

libMiMic does not have DNS client yet. Please tell me if necessary. The implementation will take some time.

Accepted Answer

Thank you for your quick response, it works for me but now I have returned buffer size "DATA" in "sendMethod" otherwise my remote server returns an error, another question is it possible to have the "Content- length in the response header returned from my remote server ?

if(http.connect(IpAddr(192,168,1,25),80)){
        
        if(http.sendMethod(HttpClient::HTTP_POST,"/ajax",34)){
            const char* DATA="{\"method\":\"lr\",\"params\":[],\"id\":1}";
            if(http.write(DATA,strlen(DATA))){
               
               
                if(http.getStatus()==200){
                    char* buf[256];
                    short len;
                    if(http.read(buf,256,len)){
                        pc.printf("%.*s",len,buf);
                    }
                }
            }
        }
        http.close();
    }    
posted by thibault buisson 26 Oct 2014

It does not exist the function to get the Content-Length.

Function read is received the streaming data from HTTP stream. It return 0 when read pointer reaches the end. For example, 256 bytes of data, will return the value of 64,64,64,64,0. (This is because that the server may use Chunked-Encoding.)

In order to receive all of the data, please implemented as follows.

if(http.getStatus()==200){
 char* buf[256];
 short len;
 do{
   if(http.read(buf,256,len)){
        pc.printf("%.*s",len,buf);
   }
 }while(len>0);
}
posted by Ryo Iizuka 27 Oct 2014