Skip to main content
I'm trying to understand the .setheader() on my HttpRequest classes better.

I'm working with an API explorer - and the header needs to look like this...

Accept : application/json

Authorization : OAuth oauth_token="************",oauth_nonce="a38cb494-be1d-461e-a878-c8389cae69a4"

So - that mean that I do MULTIPlE setHeaders?  Like this? (option A)

httpRequest request = new httpRequest();

request.setMethod('GET');

request.setHeader('Accept', 'application/json');

request.setHeader('Authorization', 'oAuth ');

request.setHeader('oauth_token', '*************');

request.setHeader('oauth_nonce,'a38cb494-be1d-4');

Or do I put them all into one string - like this? (Option B)

httpRequest request = new httpRequest();

request.setMethod('GET');

request.setHeader('Accept', 'application/json');

request.setHeader('Authorization', 'oAuth oauth_token="************",oauth_nonce="a38cb494-be1d-461e-a878-c8389cae69a4"');

So, the question is - when using the SetHeader method, do you put all of the header parameters in one string, or do you do multiple .setheaders()?

 
3 件の回答
  1. 2015年9月16日 13:20
    Hi Jeffrey,

        You can also try out by adding all request headers in the end point.

    Let's consider the below example,

    End point: https://login.salesforce.com/services/oauth2/token

    Your headers as follows,

    grant_type=password

    client_id=3MVG9A2kN3BMicExoPixGMGQ5MuS2hoPxlEHAvyVQzZVzdhClK6WUFdMY8FzXqNxc4

    client_secret=253664689064617693

    username=test@force.com

    password=testpwd3MVG9A2kN3BMicExo

    Instead of adding each headers as a separate binding, you can bind this as single string in a end point url.

     

    httpRequest request = new httpRequest();

    request.setEndPoint('https://login.salesforce.com/services/oauth2/token?grant_type=password&client_id=3MVG9A2kN3BMicExoPixGMGQ5MuS2hoPxlEHAvyVQzZVzdhClK6WUFdMY8FzXqNxc4&client_secret=253664689064617693&username=test@force.com&password=testpwd3MVG9A2kN3BMicExo');

    request.setMethod('GET');

    Hope it might helpful to you...! 
0/9000