1

I have the following service in angular 5 application:

const httpOptions = {
    headers: new HttpHeaders({ 
                'Accept': 'application/json', 
                'Content-Type': 'application/json' 
                 })
        };

addTrip(trip: Trip): Observable<any> {
    console.log('Adding trip ' + JSON.stringify(trip));
    return this.http.post(`${this.baseUrl}/Trips/trip/`, JSON.stringify(trip), httpOptions);
  }

Trip look like (in angular):

export interface Trip {
  description: string;
}

The java code is seam component which use rest api:

@Name("tripFacadeREST")
@Scope(ScopeType.EVENT)
@Path("Trips")
public class TripFacadeREST {

    @In private TripDaoHibernateImpl tripDao;

...

@POST
@Path("Trip")
@Consumes("application/json")
public Response addNewTrip(Trip newTrip) {
    tripDao.addTrip(newTrip);
    return Response.ok().entity("trip added successfully").build();
}

I can't get this call to work, I have tried also put but I understand that put is for update and because the id is created in the server side I've used post, the server side is not triggered. Any idea how to solve this problem? (I was able to use GET annotation in the same service with no issues).

7
  • 1
    does your service work using postman? Commented May 21, 2018 at 9:30
  • I haven't used postman Commented May 21, 2018 at 9:36
  • You can try postman and fiddler to see if your service works. This way you'll see if your headers are correct etc.. if postman does not work then probably your service is not working. Also it would be helpful to post the response you get from network to help us out. Commented May 21, 2018 at 9:41
  • I can see in the network tab in the browser that nothing is being sent Commented May 21, 2018 at 10:44
  • then you should start looking why your web app is not sending the request. Commented May 21, 2018 at 10:53

1 Answer 1

1

Your HttpClient code looks ok - and since you arent seeing anything in the network tab I'll bet you arent subscribing to the Observable returned by addTrip.

If you dont subscribe to the observable it will not be executed.

From the docs:

Always subscribe!

An HttpClient method does not begin its HTTP request until you call subscribe() on the observable returned by that method. This is true for all HttpClient methods.

https://angular.io/guide/http#always-subscribe

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

2 Comments

I added toPromise call and now I can see the request in the network tab but getting 500 Internal Server Error
Ok, that means that this issue is resolved. Please resolve this and open a new question - after trying to fix the new problem yourself first.

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.