0

Hi I am trying to send two objects using http post method to backend

my environment is angular4, typescirpt, asp.net MVC 5

but it is throwing 500 internal server error

the same approach if I am passing single object to the backend my backend method is getting called

here is the code with passing single object

clientSidePostCall(Results:any,Details:any):Observable<any>{
   return this._http.post(Global.ENDPOINT +'BackendMethod/',Results)
            .map((response: Response) => <any>response.json())
            .catch((err:any) => { throw err; });
        }

the above code is working fine if I send Results object to BackendMethod if it is expecting single parameter

The same code is not working if I send multiple objects to backendMethod when it is expecting two objcets.

   clientSidePostCall(Results:any,Details:any):Observable<any>{
       return this._http.post(Global.ENDPOINT +'BackendMethod/',Results,Details)
                .map((response: Response) => <any>response.json())
                .catch((err:any) => { throw err; });
            }

The above code is not working and throwing 500 internal server error

here is my backend method signature

 [HttpPost]
        public HttpResponseMessage BackendMethod([FromBody] resultsType Results, [FromBody] detailsType Details)

please help me with this

and I am having another doubt that can we pass object in http.get in angular 4 and typescript

3 Answers 3

4

In your angular code make the Results, and Details be properties of a larger object... So send this object:

const myPostBody = { resultsType: Results, detailsType: Details }

return this._http.post(Global.ENDPOINT +'BackendMethod', myPostBody)
                .map((response: Response) => <any>response.json())
                .catch((err:any) => { throw err; });
            }

Also make sure that your API class type Results actually matches the Class Results that you're sending it

Sign up to request clarification or add additional context in comments.

1 Comment

I have followed that, Thank you Kevin
3

I don't know how asp.net works but the third argument of HttpClient.post is not for another post body. In fact a post request cannot have two bodies. If your backend expects an array, do:

this._http.post(url, [Results, Details])

7 Comments

Will server will deserialize it in correct form? I doubt
Depends on the server. If it's JSON-decoding the body and expects it to be an array, yes.
Ie. whatever you pass to post() as the body, gets JSON encoded and sent as the raw post body by default.
In JSON it cannot.. An array is [1, 2, { "foo": 12} ] and object is {"a":1, "foo":"foo"}
As said, I don't know asp.net enough to tell from the code snippet what the server is expecting, an array or a few nested objects?
|
1

Wrapping up child entities in a parent entity is required for HTTP Post. The child entity would usually be a business entity. On the receiving service end, you will use the entity names as properties to receive the individual items. All this assume your entity itself is serializable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.