web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

API call throws exception on bad request

(1) ShareShare
ReportReport
Posted on by 179
Hi, 
 
I am consuming an API to send some data to an external entity. I get a 400 Bad request error and my code jumps to an exception directly and I have no idea what's wrong with the request. I sent the same request through Postman I got an error message in the response. How can get the same results as Postman in my X++ code?

Here is the sample of code that I am using to call the API.
 
System.Net.HttpWebRequest request;System.IO.Stream stream;System.Exception sysEx;request = System.Net.WebRequest::Create('URL') as System.Net.HttpWebRequest;request.Method = 'POST';request.ContentType = 'application/json';System.Net.WebHeaderCollection headerCollection = request.Headers;headerCollection.Set('Name', 'Value');var utf8 = System.Text.Encoding::get_UTF8();var byteArrayPayload = utf8.GetBytes('{key1:value1,key2:value2}');try        {using (System.IO.Stream dataStream = request.GetRequestStream()){dataStream.Write(byteArrayPayload, 0, byteArrayPayload.Length);}using (System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse){stream = response.GetResponseStream();System.IO.StreamReader reader = new System.IO.StreamReader(stream);str responseBody = reader.ReadToEnd();}}catch(sysEx){throw;}
 
I have the same question (0)
  • Judy Profile Picture
    Microsoft Employee on at
    Hi,
     
    What the error messages you got when you sent the request through postman.
    The HTTP 400 Bad Request error occurs when the request query parameter or request body content is invalid.
    Could you please check the response body to find out which part of the request is not valid.

    Best Regards,
    Judy
  • BZF_SA Profile Picture
    179 on at
    I get a proper response that the data is invalid from postman with status code 400 bad request. Thats the same I want to do in X++ to maintain a log.

    If the data is valid it works fine. 
  • Suggested answer
    Layan Jwei Profile Picture
    8,165 Super User 2026 Season 1 on at
    Hi Shahzaib,
     
    It's because you are throwing nothing in the catch.
     
    Try this
    System.Exception ex; 
    try 
    {
         //Logic
    catch(ex)
          error(ex.Message);
    }
     
    Thanks,
    Layan Jweihan
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future
  • BZF_SA Profile Picture
    179 on at
    I have tried to throw ex.message as well but its the same. All I want to achive is to get the response Json and Parse it to create a log.
    ​​​​​​​
  • Verified answer
    Anthony Blake Profile Picture
    3,092 Super User 2026 Season 1 on at
    If you can catch a System.Net.WebException, you should have access to the response body, which is what you see in postman.
     
     
    This is how to do it in C#, it should be transferrable to X++:
     
     
    System.Net.WebException ex;
    try
    {
    // call the API
    }
    catch(ex)
    {

    // read the response stream as you do on success, but from from ex.Response.GetResponseStream()
    }
     
  • Layan Jwei Profile Picture
    8,165 Super User 2026 Season 1 on at
    Hi Shahzaib,
     
    Can you please re-share your code in a proper format?
    Also did you debug and see at which line of code does it fail? And when it goes to the catch did you see in the debugger watch what ex is returning?

    ex.Message should return the error. Also did you try after the catch to put info(responseBody) and check if the result of postman appears?
     
    Thanks,
    Layan Jweihan
  • BZF_SA Profile Picture
    179 on at
    System.Net.HttpWebRequest request;
    System.IO.Stream stream;
    System.Exception sysEx;
    
    request = System.Net.WebRequest::Create('URL') as System.Net.HttpWebRequest;
    request.Method = 'POST';
    request.ContentType = 'application/json';
    
    System.Net.WebHeaderCollection headerCollection = request.Headers;
    headerCollection.Set('Name', 'Value');
    
    var utf8 = System.Text.Encoding::get_UTF8();
    var byteArrayPayload = utf8.GetBytes('{key1:value1,key2:value2}');
    
    try        
    {
    	using (System.IO.Stream dataStream = request.GetRequestStream())
    	{
    		dataStream.Write(byteArrayPayload, 0, byteArrayPayload.Length);
    	}
    	
    	using (System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse)
    	{
    		stream = response.GetResponseStream();
    		System.IO.StreamReader reader = new System.IO.StreamReader(stream);
    		str responseBody = reader.ReadToEnd();
    	}
    }
    catch(sysEx)
    {
    	throw;
    }
     
  • Suggested answer
    BZF_SA Profile Picture
    179 on at
    Thank You, Anthony Blake.
     
    your solution worked for me.
  • Anthony Blake Profile Picture
    3,092 Super User 2026 Season 1 on at
    Excellent news!
     
    Please mark my solution as verified to help future users/google searchers find their solution

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 659

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 465 Super User 2026 Season 1

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 304 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans