function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Raghu PedabbaRaghu Pedabba 

http Post Help

Dear Gurus,

I need to make POST request.

This is the JSON format for POST

{"products":[{"id":1,"quantity":1}],"desc":"infomation"}

*
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('http://*******:*****/prod/');
request.setMethod('POST');
request.setHeader('Authorization', authorizationHeader);
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
request.setBody('{"products":[{"id":1,"quantity":1}],"user_id":"user11"}');
HttpResponse response = http.send(request);
//if (response.getStatusCode() != 201) {
//    System.debug('The status code returned was not expected: ' +
//        response.getStatusCode() + ' ' + response.getStatus());
//} else {
    System.debug(response.getBody());

This works well.But I want to replace id and quantity with Integar variable.

Please guide me.

Thanks,
Raghu
 
Best Answer chosen by Raghu Pedabba
R Z KhanR Z Khan
use the following for the body of your request
String body = '{"products":[{"id":' +YOUR_ID_VAR +',"quantity":' + YOUR_QUANTITY_VAR + '}],"user_id":"user11"}'
request.setBody(body);

 

All Answers

R Z KhanR Z Khan
use the following for the body of your request
String body = '{"products":[{"id":' +YOUR_ID_VAR +',"quantity":' + YOUR_QUANTITY_VAR + '}],"user_id":"user11"}'
request.setBody(body);

 
This was selected as the best answer
Raghu PedabbaRaghu Pedabba
Thanks Khan.