question

sgarg76271 avatar image
sgarg76271 asked

HTTP request response parsing?

Hi,

I basically call an external API which has oAuth authorization.

I first made a call for the token. Got the json response back on my game.

There I parsed the json and sent back the access token string by another API call to playfab.

Is there some way to parse json on the server cloudscript itself?

Now its this URL I need to call,

Its repeatedly giving me bad request in response.

https://v2.api.xapo.com/accounts/xxxxxxx/transactions?to=xxxxxxxxxxxxxxx&amount=2&currency=SAT&notes=test&type=pay

What can be possible reason?

Do I need to provide the uri in some other way than directly in url?

Do I need to give base url seperately?

Code:

handlers.mycallApiMethod= function (args, context){

  try{
    var authorization= "Bearer " + args.accessToken;
 var headers = {
        "Authorization": authorization
    };


    var body = "all good";


    var url = "https://v2.api.xapo.com/accounts/xxxxxxxx/transactions?to=xxxxxxxxxxxxxxxxxxx&amount=2&currency=SAT&notes=test&type=pay";
    var content = body;
    var httpMethod = "post";
    var contentType = "application/json";
    var logRequestAndResponse = true;
    
    // The pre-defined http object makes synchronous HTTP requests
    var response = http.request(url, httpMethod, content, contentType,headers, logRequestAndResponse);
  return response ;
  }
  catch (ex)
  {
    log.info("had exception "+ ex);
    return ex;
}
CloudScript
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

sgarg76271 avatar image sgarg76271 commented ·

@brendan Answer this plz.

0 Likes 0 ·
brendan avatar image brendan sgarg76271 commented ·

Just so you know, we already get notifications for all posts - it's not necessary to additionally @ members of the PlayFab team.

0 Likes 0 ·

1 Answer

·
brendan avatar image
brendan answered

Yes, you can use the JSON parse() to parse a JSON formatted string into a JSON object, so that you could then use it in the Cloud Script.

I'm not familiar with xapo's specific OAuth implementation, but a 400 Bad Request could be due to almost anything being wrong in the request - a missing or bad parameter, an unsupported signature method, etc. You would need to review the specifics of their response and, if it's not clear, talk to the xapo team about why you're seeing a specific error from their service.

However, in general you would only put query strings in the URL in that manner ("?to=xxx", etc.) for a GET method. With POST, it's normally part of the body of the request. Could you try moving your to, amount, etc. into the body instead?

3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

sgarg76271 avatar image sgarg76271 commented ·

Ok I will try moving the to , amount etc to body.

Can you please give a exampe of using JSON parse in cloud script?

0 Likes 0 ·
brendan avatar image brendan sgarg76271 commented ·

Sure - it takes any string and (if it's valid JSON) returns the appropriate object. For example:

var foo = JSON.parse("{ \"a\" : 12 }");

would give you the same object as if you said

var foo = {"a" : 12};

0 Likes 0 ·
sgarg76271 avatar image sgarg76271 brendan commented ·

I got it working thanks a lot for help!

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.