HTTP POST Request Issue


#1

Hi there,

At a basic level, i’ve got a Raspberry Pi with MusicBox loaded. I’m using a second machine to change the audio using HTTP requests.

My request is - { “method”: “core.playback.get_current_tl_track”, “jsonrpc”: “2.0”, “id”: 1 } posted with the header “Content-Type”, “application/json”);"

If I use another peice of software, for example Postman, to send the string, it works.

I’m using the HTTP Request library from Dan Shiftman. The issue, i think, is when I use post.addData it requires two strings, a key and a value. I just want to send the raw string.

Any advice?

Thanks,

Sam


#2

Can you post your code?


#3

sure! When I print out, the string is formatted correctly, as in:

{“method”: core.playback.get_current_tl_track,“jsonrpc”: 2.0,“id”: 1 }

import http.requests.*;
String test = "{\"method\": core.playback.get_current_tl_track,\"jsonrpc\": 2.0,\"id\": 1 }";
public void setup() 
{
  size(400, 400);
  smooth();

  PostRequest post = new PostRequest("http://172.16.3.139:6680/mopidy/rpc");
  post.addHeader("Content-Type", "application/json");


  //curl -d '{ "method": "core.playback.get_current_tl_track", "jsonrpc": "2.0", "id": 1 }' http://172.16.3.139:6680/mopidy/rpc
  post.addData("json", test); // NOTE --- its here that's confusing me. I simply want to send the string, post.addData states that it requires two values (presumably a key and a value)??


  println(test);

  post.send();
  System.out.println("Reponse Content: " + post.getContent());
  System.out.println("Reponse Content-Length Header: " + post.getHeader("Content-Length"));
}


#4

SOLVED - I dived into the library and found addJson that accepts one single value. Inside the library i changed the header to “httpPost.addHeader(“content-type”, “application/json”);”

This fixed the issue