🎁📬❄️📦Adafruit Holiday Shipping Deadlines 2019: Please place all UPS 2 Day orders by 11am Monday, December 16 📦❄️📬🎁
0

POST request fails. Getting data missing required value
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

POST request fails. Getting data missing required value

by AmitParmar on Thu Nov 21, 2019 6:14 am

Hi,

I am trying to send HTTP POST request to adafruit IO from my ESP32 TTGO T Call. But some how request fails and returning error code 422. Below is my code and complete error details.

Error:
HTTP/1.1 422 Unprocessable Entity
"error":"request failed - Record invalid. Failed to save data to feeds/temprature, data missing required value"

Code: Select all | TOGGLE FULL SIZE
     
      String httpRequestData = "[{\"value1\":\"123\"}]";
     
      client.print(String("POST ") + resource + " HTTP/1.1\r\n");
      client.print(String("Host: ") + server + "\r\n");
      client.println("Connection: close");
      client.println("Content-Type: application/json");
      client.println("Authorization: key=my authorization key");
      client.print("Content-Length: ");
      client.println(httpRequestData.length());
      client.println();
      client.println(httpRequestData);


Same request works when i try to send request through Fiddler tool.

Can somebody please help me resolve my issue?

Thanks In Advance,
Amit

AmitParmar
 
Posts: 2
Joined: Wed Jan 03, 2018 1:37 am

Re: POST request fails. Getting data missing required value

by abachman on Thu Nov 21, 2019 12:43 pm

Hi Amit,


You're wrapping the JSON object ("{ }" symbols) in an array ("[]" symbols), which is not necessary. You're also using the key, "value1" when IO expects the key to be "value".

Also, different API endpoints use different payload formats. For example, the "Create Data" endpoint looks different from the "Create Data in a Group" endpoint (not well documented).

What value are you using for `resource` in this example? How does `httpRequestData` compare to what you're sending from Fiddler? Is it exactly the same format or is Fiddler handling JSON generation? Is it possible to see a screenshot or print out of your Fiddler setup with the API Key hidden?


- adam b.

abachman
 
Posts: 349
Joined: Mon Feb 01, 2010 12:48 pm

Re: POST request fails. Getting data missing required value

by AmitParmar on Fri Nov 22, 2019 6:39 am

HI Adam,

That worked!!! :)

Thank you so much for quickly catching the issue. As you mentioned, I removed square bracket and changed value1 to value which solved my problem. Thanks again.

i.e.
"{\"datum\": {\"value\": \"15\"}}

I have one more question.

Now, I want to send push notification to my android app as soon as new data has been added to the database. Is it possible with Adafruit io? Can you please point me out any online document if available?

Thanks,
Amit

AmitParmar
 
Posts: 2
Joined: Wed Jan 03, 2018 1:37 am

Please be positive and constructive with your questions and comments.