+ Start a Discussion
othmanothman 

Http BAD request 400 error

I have the below apex code that when called returns a bad request 400

is there anything i missed in the http request i send below?

 

@future (callout=true)	
	public static void invoke(String email, String firstName){
		Http h = new Http();
        HttpRequest req = new HttpRequest();
       	req.setMethod('POST');
			
		Blob headerValue = Blob.valueOf(USERNAME + ':' + PASSWORD);
		String authorizationHeader = 'BASIC ' +   EncodingUtil.base64Encode(headerValue);
		req.setHeader('Authorization', authorizationHeader);
		 req.setHeader('Accept', 'text/xml;charset=utf-8');	
		 	
		req.setEndpoint('http://localhost:8080/jbilling/rest/users');
				
		String requestString = '<list>'+
		'<user>'+'<userId>0</userId>'+'<userName>othmanelmoulat</userName>'+
		'<password>123qwe</password>'+'<mainRoleId>5</mainRoleId>'+
		'<contact>'+'<email>'+email+'</email>'+
		'<firstName>'+firstName+'</firstName></contact>'+
		'</user></list>';
						
		req.setHeader('Content-Length',String.valueOf(requestString.length()));
		
		req.setBody(requestString);
		HttpResponse res;
		 try {
            res = h.send(req);
		   string bodyRes=res.getBody();
		    System.debug(bodyRes);
        } catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);
            System.debug(res.toString());
        }
		
       //parse body response to extract results values
	}

 

here is the debug log of the request sf tried to process and returned 400.

is there something not right in this request? any reason why the server returns a 400?

 

18:19:43:000 USER_DEBUG POST /jbilling/rest/users/ HTTP/1.0

18:19:43:000 USER_DEBUG Content-Type: text/xml;charset=utf-8

18:19:43:000 USER_DEBUG Authorization: BASIC YWRtaW47MToxMjNxd2U=

18:19:43:000 USER_DEBUG SFDC_STACK_DEPTH: 1

18:19:43:000 USER_DEBUG Content-Length: 238

18:19:43:000 USER_DEBUG User-Agent: SFDC-Callout/25.0

18:19:43:000 USER_DEBUG Accept: text/xml;charset=utf-8

18:19:43:000 USER_DEBUG Host: localhost:8080

18:19:43:000 USER_DEBUG Via: 1.1 proxy-chi.net.salesforce.com:8080 (squid)

18:19:43:000 USER_DEBUG X-Forwarded-For: 10.232.12.20

18:19:43:000 USER_DEBUG Cache-Control: max-age=259200

18:19:43:000 USER_DEBUG Connection: keep-alive

 

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

you can't use localhost, that only makes sense on your physical host, not anywhere else, you need to use the internet resolvable name of your host or your public IP address.

All Answers

othmanothman

i have my rest service running at localhost:8080

i specify the endpoint in apex code to request my rest resource http://localhost:8080/jbilling/rest/users

 

but apex says it can't retrive specified url. is it b/c i use 'localhost:8080'? does apex understands the localhost uri ? 

what is wrong in my code above?

othmanothman

i suppose i had no replies because my apex code seems to be fine right?

if that is the case what could be giving me the HTTP BAD request 400 error? 

is there any information i need to post that my question lacks?  anyone has any idea how to help me out in this?

 

thank you.

othmanothman

here is the debug log of the request sf tried to process and returned 400.

is there something not right in this request? any reason why the server returns a 400?

 

18:19:43:000 USER_DEBUG POST /jbilling/rest/users/ HTTP/1.0

18:19:43:000 USER_DEBUG Content-Type: text/xml;charset=utf-8

18:19:43:000 USER_DEBUG Authorization: BASIC YWRtaW47MToxMjNxd2U=

18:19:43:000 USER_DEBUG SFDC_STACK_DEPTH: 1

18:19:43:000 USER_DEBUG Content-Length: 238

18:19:43:000 USER_DEBUG User-Agent: SFDC-Callout/25.0

18:19:43:000 USER_DEBUG Accept: text/xml;charset=utf-8

18:19:43:000 USER_DEBUG Host: localhost:8080

18:19:43:000 USER_DEBUG Via: 1.1 proxy-chi.net.salesforce.com:8080 (squid)

18:19:43:000 USER_DEBUG X-Forwarded-For: 10.232.12.20

18:19:43:000 USER_DEBUG Cache-Control: max-age=259200

18:19:43:000 USER_DEBUG Connection: keep-alive

 

SuperfellSuperfell

you can't use localhost, that only makes sense on your physical host, not anywhere else, you need to use the internet resolvable name of your host or your public IP address.

This was selected as the best answer
othmanothman

SimonF wrote:

you can't use localhost, that only makes sense on your physical host, not anywhere else, you need to use the internet resolvable name of your host or your public IP address.


yes that is what i suspected the problem to be. i mentioned that in my second post.