0

Trying to receive JSON RPC data from the server using Angular 4 HttpClient. The received error is {code: -32600, message: "INVALID_JSON_REQUEST: The JSON sent is not a valid JSON-RPC Request object"}.

The command is:

curl -i -X POST -d '{"jsonrpc": "2.0", "id": "1", "method": "command", "params": {"name":"get names","parameters": {"datasets": null},"destination":"http://172.16.106210:9100","source":"TESTER"}}' http://172.16.106.210:9100

Implementation on Angular application:

getDataset(dataset?: string): Observable<any[]>
  {
    const url = 'http://172.16.106.210:9100';
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');

    let params = new URLSearchParams();
    params.append("jsonrpc", "2.0");
    params.append("id", "3");
    params.append("method", "command");
    params.append("params", '{"name":"get names","parameters": {"datasets": null},"destination":"http://172.16.106.210:9100", "source":"TESTER"}');

    const options = new RequestOptions({ headers: headers, params: params });

    return this.httpClient.post(url, options)
      .map((response: any[]) => response)
      .do(response => console.log(response))
      .catch(error => Observable.throw(error));
  }

What is wrong in this JSON request?

Thank you in advance,

MariF
  • 1
  • 1
  • 1

2 Answers2

0

Try setting the Content-Type header to application/json-rpc ?

Check if it matches the format specified in this link ?

hem
  • 1,012
  • 6
  • 11
  • application/json-rpc didn't change the result: still error -32600. – MariF Nov 01 '17 at 12:15
  • This is a result I got using curl:HTTP/1.1 200 OK Connection: Keep-Alive Content-Length: 181 Access-Control-Allow-Origin: * Content-Type: application/json Date: Thu, 19 Oct 2017 15:18:23 GMT {"id":"1","jsonrpc":"2.0","result":{"datasets":["Boards Dataset","MGE6000 Board Dataset"],"description":"OK","fcode":16777216,"name":"get names","source":"BrdMngr","status":"ACK"}} – MariF Nov 01 '17 at 12:17
  • Using Fiddler to check the transferred data: surprise! - no data sent!. No header, no body. – MariF Nov 01 '17 at 13:34
  • Check in the chrome console if the request is actually fired ? – hem Nov 01 '17 at 14:53
  • The problem is solved: httpClient.post(url, body, headers) - where body is a string with "jsconrpc", "id", "method", "params". Meantime, URLSearchParams and RequestOptions are not in use. Thank you for your help – MariF Nov 02 '17 at 13:16
0

Working example based on the discussion here:

@Injectable({
  providedIn: 'root'
})
export class Service {
  public encoder: HttpParameterCodec;
  public defaultHeaders = new HttpHeaders();
  constructor(protected httpClient: HttpClient) {}


public createBriefing(
briefingData: Briefing //: Observable<any>
) {
const url = "some.url";
let headers = new HttpHeaders();
headers.append("Content-Type", "application/json");

let body= new JsonRpcBody();
body.id = briefingData.id;
body.method = "query";
body.params = briefingData.params;  

this.httpClient
  .post(url, JSON.stringify(body), { headers: headers })
  .subscribe(response => console.log(response));
 }
}

export class JsonRpcBody {
    id:string;   
    method: string; 
    params: BriefingParameters[]
}

export class BriefingParameters {
id: string;
reportTypes: string[]
stations: string[]
countries: string[]
}
niio
  • 326
  • 3
  • 15