Submitting a nested JSON body in ASP MVC

I'm currently struggling to send a nested JSON body to an API, and I've tried a few different approaches without success. The JSON values are sourced from multiple models, making it quite complex for me to handle. Any assistance or guidance on this matter would be greatly appreciated.

public Object getfreight()
    {
         var json = "{" +
"'from': {" +
    "'postcode': '3207'" +
"}," +
"'to': {" +
    "'postcode': '2001'" +
"}," +
"'items': [" +
    "{" +
        "'length': 5," +
        "'height': 5," +
        "'width': 5," +
        "'weight': 5," +
        "'item_reference' : 'abc xyz'," +
        "'features':{" +
            "'TRANSIT_COVER': {" +
                "'attributes': {" +
                    "'cover_amount':1000" +
                "}" +
            "}" +
        "}" +
    "}" +
"]" +
"}";

 string url = 
"https://digitalapi.auspost.com.au/postage/parcel/domestic/calculate.json?length=22&to_postcode=3083&from_postcode=3076&weight=2&height=40&width=50&service_code=AUS_PARCEL_REGULAR";


         //synchronous client.
        var client = new WebClient();
        var serializer = new JavaScriptSerializer();
        client.Credentials = new NetworkCredential("***", "***"); 

        client.Headers.Add("Content-Type: application/json");
        client.Headers.Add("Accept: application/json");
        client.Headers.Add("account-number: ***");
        var data = serializer.Serialize(json);
        var content = client.UploadString(url,json);

        var jsonContent = serializer.Deserialize<Object>(content);
        return jsonContent;
    }

Answer №1

Having trouble using jQuery to send your json object to the API?

 function Search () {
       try {
                    var req = {
                        length:"5",
                        Higet: "10",
                        item_reference: "",  
                    }

                    $.ajax({
                        url: https://digitalapi.auspost.com.au/postage/parcel/domestic,
                        type: 'POST',
                        data: req,
                        dataType: "json",
                        async: true,
                        success: function (result) { 


                        },
                        error: function (e) {

                        }
                    });
                }
                catch (e) {

                }
            }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Activate the 'swipe back' feature within ion-item upon selecting ion-option-button

In my Ionic project, I created an ion-list with ion-items and added ion-option-buttons: <ion-list> <ion-item class="item " ng-repeat="exercise in exercises" on-double-tap="sendToChangeExercise"> <p><h2>{{exercise. ...

Organize Dates in React Table

I need help with sorting the Date column in my code. Currently, the sorting is being done alphabetically. Here is the JSON data and code snippet: JSON [ { "date": "Jun-2022" }, { "date": "Jul-2022" } ...

Tips on activating the CSS style while typing using the onChange event in React

Is it possible to dynamically adjust the width of an input as we type in React? Currently, my input has a default width of 1ch. I would like it to increase or decrease based on the number of characters entered, so that the percentage sign stays at the end ...

AJAX cached outcomes

Trying to educate myself on AJAX using w3schools.com, but struggling with a particular example: xhttp.open("GET", "demo_get.asp", true); xhttp.send(); In the above example, there might be a cached result. To prevent this, you can include a unique ID in t ...

Converting PHP Parameters to C#Syntax

I have a string that includes parameters like ?action=a&current=b&something=c. How can I extract the value of 'current' to create a variable in C#? Is there a built-in method or should I use string.split and write my own function? -edit- ...

What is the proper method for storing user registration information in the database so that it can be easily retrieved and accessed later? (Error message)

User.cs: An error is occurring in the User class where 'User' is not a valid type in the context of TestBlazor project. It happened when attempting to save user registration data. using System; using System.Collections.Generic; using S ...

Unsuccessful Invocation of Servlet by Ajax Function

I have a situation where I am trying to trigger an Ajax javascript function from my jsp file, with the intention of loading a servlet for further processing. The issue I am facing is that even though I am able to pass values from the jsp to the ajax functi ...

What is the best way to implement nested iterations in Jade?

ul li a(href='') menu1 li a(href='') menu2 ul li a(href='') sub-menu2 li a(href='') menu3 ul li a(href=&apos ...

Having issues with image hover and click functionality

My website has an image with the cursor set to pointer and a function that should show a hidden element when clicked. However, it's not working at all. When I hover over it or click on it, nothing happens. Here is a screenshot of the issue along with ...

How can I retrieve the value of a div nested within another div?

Alright, let's talk about a situation where we have a draggable HTML div element: <div id="server" draggable="true" ondragstart="return dragStart(event)">Server</div> and also a target div: <div id="target1" ondragenter="return dragE ...

Once the ajax request is finished, load only the <script> tags that have specific ids

I'm currently implementing infinite-scroll to dynamically load more content which includes <script> tags that need to be executed. To achieve this, I have created the following code as an ajax-callback: JS on load ajax callback: function a ...

Application utilizing Meteor to connect with external websites or applications

Hey everyone, I'm in the process of developing an application that features a directory of stores. One key requirement is that each store has a unique view created with either Flash or JavaScript. The special view components have already been develope ...

Place attribute value directly under the border of an HTML element using a combination of JavaScript and CSS

I'm currently working on a JavaScript script to scan the DOM for elements that have a specific custom attribute called example-type. The goal is to apply CSS styling to draw a border around these elements and then display the value of the example-type ...

Unable to serve certain file extensions from the .NET Core local server even after making changes to the <requestFiltering> section in the applicationHost.config file

I am currently working on a website and encountering an issue with accessing/downloading certain file extensions such as .py and .cs. For instance, when attempting to fetch a .cs file, the following code is used: fetch("https://localhost:44310/userdat ...

What is the process for pulling out a specific JSON element based on a condition?

I am working with a JSON object that looks like this: "links" : [ { "rel" : "first", "href" : "http://localhost:8080/first" }, { "rel" : "self", "href" : "http://localhost:8080/self" }, { "rel" : "next", "href" : "http://loca ...

Incorporating a personalized image to create custom icons on the Material UI bottom navigation bar

Is it possible to replace the default icon with a custom image in material ui's BottomNavigation component? I'm curious if Material UI supports this feature. If you'd like to take a closer look, here is the link to the codesandbox. ...

unable to submit form using angular and express

I am encountering an issue with a post request in AngularJS to express. The HTML form on the client side is defined as follows: <div ng-controller="LoginCtrl"> <form ng-submit="login()"> <div> <label>Username</lab ...

Just for laughs: "The react-redux context value seems to be playing hide and seek; make sure to wrap the component in a <Provider> for it to show up!"

I encountered an issue while attempting to run a basic Jest test on a form React component: ● <LoginForm /> › clicking the validate button › should display page title ...

Retrieve a specific record from a JSON string using MySQL

My current approach involves storing data as a JSON string in MySQL due to the array of advantages it provides for my specific situation. These JSON strings can often become lengthy and contain multiple entries with unique IDs. As of now, I retrieve the e ...

Implementing a language switch feature for text display in Node.js and Next.js websites

One of the features my client is requesting for a web app is dual language support, where users can easily switch between French and English. To address this requirement, I incorporated a button that toggles a state and saves the user's language pref ...