3

I want to create share url of a file which is in Box.com using REST API from Apex.

I have written following code

public static Map<Integer, boxShareUrlJson> getShareUrl(String accesstoken,string itemid){
         String endpoint = 'https://api.box.com/2.0/files/'+itemid;
         String token =  EncodingUtil.urlEncode(accesstoken,'UTF-8');
         Httprequest request = new Httprequest();
         request.setMethod('PUT');
         request.setHeader('Authorization','Bearer '+accesstoken); 
         request.setHeader('Content-Type','application/json');
         request.setEndpoint(endPoint);
         String postbody = '{"shared_link":"{"access":"open"}}';
         request.setBody(postbody);
         Http http = new Http();
         System.debug(request+'~~~~~~~~~~~~~~~~box share request'+request.getBody());
         Httpresponse ApiResponse = http.send(request);
         Jsonparser parser = Json.createParser(ApiResponse.getBody());
         boxShareUrlJson ContentClass = new boxShareUrlJson(parser);
         Map<Integer,boxShareUrlJson> resultMap = new Map<Integer,boxShareUrlJson>();      
         resultMap.put(ApiResponse.getStatusCode(),ContentClass);
         return resultMap;


  }

But when I Execute this code I received following error :

System.HttpResponse[Status=Bad Request, StatusCode=400]

Please suggest me where I am wrong or missing something?

Thanks in advance

5

You're properly sending a bad request! Obvious typo: postbody holds an invalid JSON, there's an excessive double quote (").

| improve this answer | |
1

From the official Box documentation, the '400' status code is returned when

The request is missing a required parameter, includes an unsupported parameter or parameter value, repeats the same parameter, uses more than one method for including an access token, or is otherwise malformed

As endrju pointed above, there's an extra double quote in your POST body. If fixing that still does not resolve the issue, make sure your access token is correct. You can do that by firing a CURL request with same params as in the above code snippet - it's a good way to verify that you are setting all parameters correctly

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.