0

I'm trying to send an http request to a remote web service, the web service requires I POST an httpheader formatted as { "username" : "theusername", "password" : "thepassword" } so I've created a string variable named loginstring that is formatted as '"theusername", "password" : "thepassword" }' and set my header request as req.setHeader('{ "username" : ', loginstring); I'm getting a System.CalloutException: Attempt to use invalid header in request. I'm trying to figure out if this error is coming from sfdc or the target web service, from the debug log I'm seeing 0 of 100 callouts so I'm assuming that the error is coming from sfdc.

  if(loginstring != null){

    HttpRequest req = new HttpRequest();
    HttpResponse res = new HttpResponse();
    Http http = new Http();

    req.setMethod('POST' ); // Method Type

    req.setEndpoint('web210.ntree.com/twig-cstc3/v1/login');//salestree url

    req.setHeader('Content-Type','application/json');
    req.setHeader('{ "username" : ', loginstring);


        res = http.send(req);

        if(res.getBody() != null){

Is this an sfdc side error and if so what am I doing wrong?

1
  • You cannot just set anything as HTTP header..These are the valid values..cs.tut.fi/~jkorpela/http.html. Try setting the header as ` req.setHeader('{ "Authorization" : ', loginstring);`
    – javanoob
    Aug 26, 2015 at 13:44

1 Answer 1

1

Seems like you are missing the HTTP header name and that you might not have the exact formatting required for the header value. You should consult the web service documentation and/or contact their support if necessary.

That said I would guess one of the following are true:

  1. The JSON you provided is meant to be used with basic authentication header: req.setHeader('Authorization', 'BASIC ' + [base64-encoded JSON credentials string you provided]);

  2. You need to use two separate HTTP request headers: req.setHeader('username', 'theusername'); req.setHeader('password', 'thepassword'); `

  3. You need a vendor-specified header name and value formatting/encoding that you don't know yet.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .