Go Down

Topic: HTTP POST Error Problem with Parameter json_data (Read 1 time) previous topic - next topic

wanderjose

Hello everyone,

I am doing a project with an Arduino UNO and an Ethernet Shield, It consists of doing an HTTP POST from a JSON to a Server, I have reviewed my code many times and I still have the same error, the server receives my POST but it tells me that there is a missing parameter.

However I am sending the parameter I have tried many ways that I have seen all over the internet but I still do not get a solution

My Code :

Code: [Select]
#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC CUALQUIERA
byte server[] = {172, 30, 31, 6}; //IP SERVIDOR
byte ip[] = {172, 30, 36, 32}; // IP ARDUINO



EthernetClient client; // DEFINIMOS EL CLIENTE


void setup() {
// INICIO DEL SETUP ################################################################
Serial.begin(9600);  // INICIALIZAR MONITOR SERIAL

while(!Serial){
  ;
}  // ESPERAR POR EL MONITOR SERIAL
SPI.begin();
Ethernet.begin(mac, ip); //Conexion Por ip Estatica
Serial.print("My IP : ");
Serial.println(Ethernet.localIP());

delay(500);

if (client.connect(server, 80)) {   //comprobar si hay conexion con el cliente
  Serial.println("conectado a 172.30.31.6");
}else{
  Serial.println("NO HAY CONEXION");
  }

char PostData[] = "{\"operation\":\"core/create\",\"class\":\"UserRequest\",\"output_fields\":\"id\",\"comment\":\"loquesea\",\"fields\":{\"org_id\":\"1\",\"title\": \"prueba_hospital\",\"description\":\"prueba\"}}";


      if (client) {
          client.println("POST /itop/web/webservices/rest.php?version=1.1&json_data= HTTP/1.1");
          client.println("Host: 172.30.31.6");
          client.println("Authorization: Basic YWRtaW46YWRtaW4="); // User and pass in Base64
          client.println("User-Agent: Arduino/1.8.5");
          client.println("Content-Type: application/json");
          client.print("Content-Lenght: ");
          client.println(strlen(PostData));
          client.println("Connection: close");
          client.println();
          client.print(PostData);
          Serial.println("Post Realizado");
        }else {
          Serial.println("No se ha podido hacer el POST");
        }


} // FINAL DEL SETUP #########################################################



void loop() { // INICIO DEL LOOP ####################################################################

        if(client){
          char c = client.read();
          Serial.print(c);
        }
     
delay(10);

}






In the first line of the post, I am sending the parameters:
version = 1.1 & json_data =
probe send the json_data down along with the json himself, but it has not worked either



  client.println("POST /itop/web/webservices/rest.php?version=1.1&json_data= HTTP/1.1");



Response :


Code: [Select]


My IP : 172.30.36.32
conectado a 172.30.31.6
Post Realizado


Date: Wed, 02 May 2018 21:12:56 GMT
Server: Apache/2.4.25 (Debian)
Set-Cookie: itop-cccc625803db2ea97734aa176e552d97=oh2ru9fe93p13jvqes0sfek446; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-cache
Pragma: no-cache
Access-Control-Allow-Origin: *
Content-Disposition: inline; filename=""
Content-Length: 58
Connection: close
Content-Type: application/json

{"code":3,"message":"Error: Missing parameter 'json_data"}




Thank you all, I hope you can find the error where I can not find it

wanderjose

proved send the json_data down along with the json itself, but it has not worked either *

Sorry spelling mistake.

Juraj

remove json_data= from the url. you send it in the POST's body

wanderjose


I tried, this way


char PostData[] =  "&json_data={\"operation\":\"core/create\",\"class\":\"UserRequest\",\"output_fields\":\"id\",\"comment\":\"loquesea\",\"fields\":{\"org_id\":\"1\",\"title\": \"prueba_hospital\",\"description\":\"prueba\"}}"


and


char PostData[] ="json_data={\"operation\":\"core/create\",\"class\":\"UserRequest\",\"output_fields\":\"id\",\"comment\":\"loquesea\",\"fields\":{\"org_id\":\"1\",\"title\": \"prueba_hospital\",\"description\":\"prueba\"}}"




but I still do not get positive results

Juraj

your Content-Type is application/json, so the body of the request must be json only. if you want to send the data in body in a form of url parameters, use Content-Type application/x-www-form-urlencoded

wanderjose

I am using this =


char PostData[] = "{\"operation\":\"core/create\",\"class\":\"UserRequest\",\"output_fields\":\"id\",\"comment\":\"loquesea\",\"fields\":{\"org_id\":\"1\",\"title\": \"prueba_hospital\",\"description\":\"prueba\"}}";

      if (client) {
          client.println("POST /itop/web/webservices/rest.php?version=1.1 HTTP/1.1");
          client.println("Host: 172.30.31.6");
          client.println("Authorization: Basic YWRtaW46YWRtaW4="); // USUARIO Y CONTRASE√ĎA EN BASE 64
          client.println("User-Agent: Arduino/1.8.5");
          client.println("Content-Type: application/json");
          client.print("Content-Lenght: ");
          client.println(strlen(PostData));
          client.println("Connection: close");
          client.println();
          client.print(PostData);
          Serial.println("Post Realizado");
        }else {
          Serial.println("No se ha podido hacer el POST");
        }



Sending =

POST /itop/web/webservices/rest.php?version=1.1 HTTP/1.1
Host: 172.30.31.6
Authorization: Basic YWRtaW46YWRtaW4=
User-Agent: Arduino/1.8.5
Content-Type: application/json
Content-Lenght: 165
Connection: close

{"operation":"core/create","class":"UserRequest","output_fields":"id","comment":"loquesea","fields":{"org_id":"1","title": "prueba_hospital","description":"prueba"}}





Response =

HTTP/1.1 200 OK
Date: Thu, 03 May 2018 15:32:31 GMT
Server: Apache/2.4.25 (Debian)
Set-Cookie: itop-cccc625803db2ea97734aa176e552d97=7qgb59ot28bol9tmp4u29h5825; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-cache
Pragma: no-cache
Access-Control-Allow-Origin: *
Content-Disposition: inline; filename=""
Content-Length: 58
Connection: close
Content-Type: application/json

{"code":3,"message":"Error: Missing parameter 'json_data"}

Juraj

it is something that the web service expects and we don't know about it. what service is it?

wanderjose

a web service is iTop,

if i copy and paste this in the url

http://172.30.31.6/itop/web/webservices/rest.php?version=1.1&auth_user=admin&auth_pwd=admin&json_data={"operation":"core/create","class":"UserRequest","output_fields":"id","comment":"loquesea","fields":{"org_id":"1","title":"prueba_hospital",
"description":"prueba"}}

the server accept the request.

i need replicate that same in the POST of the arduino

Juraj

again, if you want to send the data in body in a form of url parameters, use Content-Type application/x-www-form-urlencoded

wanderjose


He did catch traffic with wireshark, and analyzed the packages that they send with the mailman, to see the http headers that he uses.

Guiding me a little there, my code goes like that, but it still does not work, I do not like it and I like the goal or going away


char PostData[] = "{\"operation\":\"core/create\",\"class\":\"UserRequest\",\"output_fields\":\"id\",\"comment\":\"loquesea\",\"fields\":{\"org_id\":\"1\",\"title\": \"prueba_hospital\",\"description\":\"prueba\"}}\r\n";


      if (client) {

          client.println("POST /itop/web/webservices/rest.php? HTTP/1.1");

          client.print("Host: 172.30.31.6\r\n");

          client.print("Authorization: Basic YWRtaW46YWRtaW4=\r\n"); // USUARIO Y CONTRASE√ĎA EN BASE 64
         
          client.print("User-Agent: Arduino/1.8.5\r\n");

          client.print("Content-Lenght: ");

          client.print(strlen(PostData));

          client.print("\r\n");

          client.print("Content-Type: application/json\r\n");

          client.print("Accept: */*\r\n");

          client.print("Connection: keep-alive\r\n");

          client.print("Content-Disposition: form-data; name=\"version\"\r\n");

          client.print("\r\n");

          client.print("1.1\r\n");

          client.print("\r\n");

          client.print("Content-Disposition: form-data; name=\"json_data\"\r\n");

          client.print("\r\n");

          client.print(PostData);

          client.print("\r\n");

          Serial.println("Post Realizado");

        }else {
          Serial.println("No se ha podido hacer el POST");
        }





i try with
          client.print("Content-Type: application/json\r\n");

and

          client.print("Content-Type: application/x-www-form-urlencoded\r\n");

But i have not got possitive results

Juraj

show the complete url-encoded vetsion of code. was the version parameter in the body too?

wanderjose


version 1.1 is a necessary parameter for the web service, independent of HTTP 1.1 are different things, so I pass it as a parameter like the json, and certainly not taking it.

Juraj

POST with all url parameters in the body and x-www-form-urlencoded is equivalent to GET with the same url parameters

wanderjose

try to pass version = 1.1 as a parameter like json_data, but the server does not receive it either and it gives me another similar error:

{"code": 2, "message": "Error: Missing parameter 'version}

that is, you are not receiving that parameter either

Juraj

show the version of your code with x-www-form-urlencoded

Go Up