aurelia / http-client

A simple, restful, message-based wrapper around XMLHttpRequest.
MIT License
62 stars 59 forks source link

is http.put() fully functional? #73

Closed adriatic closed 9 years ago

adriatic commented 9 years ago

I was advised (some time ago) that rather than using the defaults in

save(movie) {
        return this.http.put(baseUrl, movie);
    }

I should use the request builder to be very specific:

save(movie) {
        var request = this.http.createRequest();
        request.asPut()
               .withUrl(baseUrl)
               .withHeader("Accept", "application/json")
               .withHeader("Content-Type", "application/json")
               .withContent(movie);

        return request.send().then(response => response.content);
    }

Is this still the case? The reason for this question is that when using the first, more generic form I get

Unhandled promise rejection Error: A value is required for route parameter 'id' in route 'details'.
    at RouteRecognizer.generate (http://localhost:3000/jspm_packages/github/aurelia/route-recognizer@0.6.0/index.js:335:17)
    at AppRouter.generate (http://localhost:3000/jspm_packages/github/aurelia/router@0.10.2/index.js:1053:34)
    at eval (http://localhost:3000/movies/edit.js!eval:42:53)
    at run (http://localhost:3000/jspm_packages/npm/core-js@0.9.18/modules/es6.promise.js:91:43)
    at http://localhost:3000/jspm_packages/npm/core-js@0.9.18/modules/es6.promise.js:105:11
    at module.exports (http://localhost:3000/jspm_packages/npm/core-js@0.9.18/modules/$.invoke.js:6:25)
    at queue.(anonymous function) (http://localhost:3000/jspm_packages/npm/core-js@0.9.18/modules/$.task.js:40:9)
    at Number.run (http://localhost:3000/jspm_packages/npm/core-js@0.9.18/modules/$.task.js:27:7)
    at listner (http://localhost:3000/jspm_packages/npm/core-js@0.9.18/modules/$.task.js:31:9)
EisenbergEffect commented 9 years ago

I'm not sure it's related to the error. The error you are getting is coming from the router when it is trying to generate a route. It's saying that you tried to generate a parameterized route but didn't supply the parameter value to generate it from.

adriatic commented 9 years ago

The more specific version of save works -- and from the viewpoint of what data is provided, both versions are equivalent - as shown below image

adriatic commented 9 years ago

Somehow, I managed (did I) to close this issue, when that was not my intent.

I believe that the problem is in generic form where the two headers "Accept", "application/json" and "Content-Type", "application/json" are not assumed and probably should if I am right believing that this should be default.

EisenbergEffect commented 9 years ago

I would check the response.content with the configured request to see if you are getting back the data you expect and in the form you expect it.

adriatic commented 9 years ago

Already did that - when using the specific version of put, the results I am getting are OK

EisenbergEffect commented 9 years ago

@bryanrsmith Can you see anything wrong with the code above. It looks fine to me. I'm not sure why there would be a problem with the http service like this. I believe this has solid test coverage. The error is related to route generation though. Perhaps something is going wrong there?

@adriatic Can you show me the code that calls your save method?

bryanrsmith commented 9 years ago

Try removing the .then() continuation in the second version. The first version is returning the response, but the second is returning the response content, so the shape of the return values is different. On Tue, Jul 21, 2015 at 7:45 AM Rob Eisenberg notifications@github.com wrote:

@bryanrsmith https://github.com/bryanrsmith Can you see anything wrong with the code above. It looks fine to me. I'm not sure why there would be a problem with the http service like this. I believe this has solid test coverage. The error is related to route generation though. Perhaps something is going wrong there?

@adriatic https://github.com/adriatic Can you show me the code that calls your save method?

— Reply to this email directly or view it on GitHub https://github.com/aurelia/http-client/issues/73#issuecomment-123350434.

adriatic commented 9 years ago

Sure:

import {inject} from "aurelia-framework";
import {MovieData} from "./movieData";
import {Router} from "aurelia-router";

@inject(MovieData, Router)
export class Edit {

    constructor(movieData, router) {
        this.data = movieData;
        this.router = router;
    }

    activate(params) {
        return this.data.getById(params.id)
                   .then(movie => {
                       this.movie = movie;                     
                   });
    }  

    save() {       
        this.data.save(this.movie)
            .then(movie => {
                let url = this.router.generate("details", { id: movie.id});
                this.router.navigate(url);
            });      
    }
}
EisenbergEffect commented 9 years ago

@bryanrsmith Second pair of eyes helps.