0

I'm trying to send a post request. But the API returns undefined, and I send it to my API, as it turned out [object Object]

getAccouting():Observable<any>{
    // let json = this.http.get<any>('/assets/cities/altayskiy_kray/accounting.json'); if i use it json return undefined
    let json;
    this.http.get('/assets/cities/altayskiy_kray/accounting.json').pipe(data=>json=data);
    let formData:FormData = new FormData;
    formData.append('json', json);
    return this.http.post<any>('http://api/api.php', formData);
  }

How fix that?

6
  • Does this answer your question? Sending two HTTP Requests one after each other using Angular / RXJS where second request requires the first? Commented Dec 16, 2021 at 13:55
  • See the second answer for how to use a pipe this.http.get().pipe( mergeMap(response: Response => this.http.get(response.doSomething()) ). The docs for switchMap() operator are a good starting point too learnrxjs.io/learn-rxjs/operators/transformation/switchmap Commented Dec 16, 2021 at 13:57
  • Christoph, no it is not answer, that not work :( Commented Dec 16, 2021 at 14:33
  • "does not work" will not help to provide you a better answer. The linked answer shows the basics of how to do two async http calls and you really need to understand the basics. Assuming you now know how to do multiple http calls, how does your code looks like now and what exactly isn't working? Commented Dec 16, 2021 at 15:03
  • If i use this.http.get(), then get the error "an argument for the uri was expected". And if I add, as indicated above, then where mergeMap throws the error: "Type argument"(json: ArrayBuffer) => void" cannot be assigned to a type parameter "(value: ArrayBuffer, index: number) => ObservableInput<any>". The type "void" cannot be assigned to the type "Observable Input<any>".ts(2345)", but besides I wrote "mergeMap(json=>" Commented Dec 16, 2021 at 15:21

1 Answer 1

1

http.get is async so it will not have emitted before you return the http.post. Therefore, json will be undefined.

Instead you can chain them and return the whole observable:

getAccouting(): Observable<any>{
  return this.http.get('/assets/cities/altayskiy_kray/accounting.json').pipe(
    switchMap(json => {
      return this.http.post<any>('http://api/api.php', json);
    }
  );
}

Do you need to use FormData? If you append the json object then you will get its string value: [object Object].

You could stringify the json object, but you will have to decode it on the server:

formData.append('json', JSON.stringify(json))
Sign up to request clarification or add additional context in comments.

5 Comments

For some reason, I now need the json variable in FormData.append('json', json);
Do you need to use FormData?
I do not know, but without it, the request is not sent
So you could append each key-value from the json object to the FormData. Or you could use formData.append('json', JSON.stringify(json)) but then you'd have to decode it on the other end.
You're welcome - glad it solved your issue :)

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.