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

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

sending JSON to exeternal API

(0) ShareShare
ReportReport
Posted on by 942

Hello,

I try to send JSON to external API Like that

public void createAndSendJSON()
    {
        System.Net.WebClient webClient;
        System.Text.UTF8Encoding encoder;
             
        try
        {
            webClient = new System.Net.WebClient();
            System.Net.WebHeaderCollection headers = webClient.Headers;
            headers.Add("Content-Type", "application/json");
            encoder = new System.Text.UTF8Encoding();
            str json = FormJsonSerializer::serializeClass(HDCSendSalesQuotationRequest);// use this AX helper class (FormJsonSerializer) to convert to JSON here
            info(json);
            System.Byte[] encodedBytes = encoder.GetBytes(json);
            str APILink = "https://myapi.azurewebsites.net/Approve";
            /*System.Byte[] response =*/ webClient.UploadData(APILink, encodedBytes);
            //str jsonResponse = webClient.Encoding.GetString(response);
            // deserialize result returned from API here using FormJsonSerializer::deserializeClass();
        }
        catch (Exception::CLRError)
        {

            throw error(AifUtil::getClrErrorMessage());
        }
    }

contract class code is

[DataContractAttribute]
class HDCSendSalesQuotationRequest
{
    str         quationId;
    boolean     approvedOption;
    str         comment;
    CustAccount custAccount;
 
    [DataMemberAttribute]
    public str quationId(str _quationId = quationId)
    {
        quationId = _quationId;
        return quationId;
    }

    [DataMemberAttribute]
    public boolean approvedOption(boolean _approvedOption = approvedOption)
    {
        approvedOption = _approvedOption;
        return approvedOption;
    }

    [DataMemberAttribute]
    public str comment(str _comment = comment)
    {
        comment = _comment;
        return comment;
    }

}

I constantly get Bad Request error

' The remote server returned an error: (400) Bad Request.'

The format of JSON that is waited by the API is 

{
  "quationId": "string",
  "approvedOption": true,
  "comment": "string"
}

json my code sends to API is

{
"approvedOption":false,
"comment":"",
"quationId":"000030"
}

the order is incorrectI but I do not understand how to handle the order of parameters in generated JSON so that it is equal to the one needed. 

If you have any ideas please share them with me?

I have the same question (0)
  • Martin Dráb Profile Picture
    237,924 Most Valuable Professional on at

    Your JSON looks correct to me. What if you try to call the service through Postman or something? Does it work? If not, the problem isn't in your code, but in some expectations.

    Does the service expects a POST request? Does it need some authentication?

    By the way, let me simplify your code a bit:

    public void createAndSendJSON()
    {
    	System.Exception ex;
    
    	try
    	{
    		str json = FormJsonSerializer::serializeClass(hdcSendSalesQuotationRequest);
    		info(json);
    		
    		System.Net.WebClient webClient = new System.Net.WebClient();
    		webClient.Encoding = System.Text.Encoding::UTF8;
    		
    		System.Net.WebHeaderCollection headers = webClient.Headers;
    		headers.Add("Content-Type", "application/json");
    		
    		str apiLink = "https://myapi.azurewebsites.net/Approve";
    		webClient.UploadString(apiLink, json);
    	}
    	catch (ex)
    	{
    		throw error(ex.Message);
    	}
    }

  • dark_knight Profile Picture
    942 on at

    thanks. does the order of parameters matter?

  • Martin Dráb Profile Picture
    237,924 Most Valuable Professional on at

    Normally it shouldn't, but you can't ask us about how your "external API" is implemented. We can't know.

    If it was developed by yourself, you're the only one who knows how it works.

    If it was developed by someone else, consult its documentation, ask the owner or simply go and test it.

  • dark_knight Profile Picture
    942 on at

    OK. Thank you.

  • dark_knight Profile Picture
    942 on at

    the API isn't written by me. I need to prove somehow that problem isn't in my code. Is there any way to somehow change the order of parameters in my JSON? or the only way is to reparse the json string? I don't think it'll help but I need to show that order doesn't matter.

  • Martin Dráb Profile Picture
    237,924 Most Valuable Professional on at

    Don't invest time to changing the order of properties if you don't know whether it's a problem or not. Make a request from Postman and use the JSON that (according to you) should be accepted:

    {
    	"quationId":"000030",
    	"approvedOption":false,
    	"comment":""
    }

    If it fails, you already know that your current problem is caused by something else.

    If it works, try the different order:

    {
    	"approvedOption":false,
    	"comment":"",
    	"quationId":"000030"
    }

    If it still works, you know that the ordering isn't a problem.

    If it the former works and the latter doesn't, it means that the services depends on a particular order. I would consider it a bug, therefore maybe you should ask to get it fixed.

    Only when you make sure that the problem is caused by the order of properties and you want to solve it on your side rather than in the service, there will be time to ask how to change the order.

  • dark_knight Profile Picture
    942 on at

    thank you very much Martin. Quite often you are my only help here. Is postman some kind of utility I need to install on y devbox? never used it before. sorry

  • Martin Dráb Profile Picture
    237,924 Most Valuable Professional on at

    Yes, it's a Windows application. It can do much more, but what we're interested in in this case is the ability to easily send requests and see responses. Check out Postman's web page for more information.

    It's not necessary (you can write code to make test requests), but it makes these situations much easiers.

    Obviously, Postman isn't the only tool for this purpose, but it's very common. To give you another example, you could use Fiddler instead.

  • dark_knight Profile Picture
    942 on at

    you were absolutely right. it is nothing to do with json parameters order. check out in Postman

    this doesn't work returning the same error 500

    {
    	"quationId":"000030",
    	"approvedOption":false,
    	"comment":""
    }

    thanks again Martin

  • Martin Dráb Profile Picture
    237,924 Most Valuable Professional on at

    Note that you said that the error code with 400, not 500.

    4xx error codes usually mean that something is wrong with your requests, while 5xx means that something failed on the server.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 559 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 464 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans