1

I try to send the json object to rest services but I get some error like this:

http://localhost:8080/api/v1/cardLimit 400 (Bad Request);

Wrap to JSON

 public class GameLimit implements Serializable {

    private static final long serialVersionUID = 1L;

    private LimitType firstLimit;
    private LimitType secondLimit;

    public LimitType getFirstLimit() {
        return firstLimit;
    }

    public void setFirstLimit(LimitType firstLimit) {
        this.firstLimit = firstLimit;
    }

    public LimitType getSecondLimit() {
        return secondLimit;
    }

    public void setSecondLimit(LimitType secondLimit) {
        this.secondLimit = secondLimit;
    }
}


public class LimitType implements Serializable{

    private static final long serialVersionUID = 1L;

    private BigDecimal limit;
    private String type;
    private String status;

    public BigDecimal getLimit() {
        return limit;
    }

    public void setLimit(BigDecimal limit) {
        this.limit = limit;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

Limit request object

public class LimitReq extends GameLimit {

    private String key;

    public String getKey() {
        return key;
    }
}

RestController:

@RequestMapping(value = "/GameLimit", method = RequestMethod.POST)
    public Response setCardLimit(@RequestBody GameLimitReq request) throws Exception {
        return limitService.updateGameLimit(request);
    }

TypeScript client:

changeLimits(firstLimit: IWidgetLimit, secondLimit: IWidgetLimit, key: string): ng.IPromise<any> {
            return this.$http.post(this.apiPrefix + '/GameLimit', {
                'firstLimit': {
                    limit: firstLimit.limit,
                    type: firstLimit.type,
                    status: firstLimit.status
                },
                'secondLimit': {
                    limit: secondLimit.limit,
                    type: secondLimit.type,
                    status: secondLimit.status,
                },

                key: key
            }).then(function (response: any) {
                return response.data;
            }.bind(this));
        }
Gabriel
  • 1,922
  • 2
  • 19
  • 37
tiger
  • 23
  • 5
  • What does the request look like in the browser console/network tab? does your object get posted correctly? – Alex Jan 23 '17 at 19:34
  • @Alex R no, i get error like this http://localhost:8080/api/v1/cardLimit 400 (Bad Request); if i set to null 'firstLimit': null, 'secondLimit':null for everyone objects which i try ro send it with succesful deal but if i set the object i get the error like http://localhost:8080/api/v1/cardLimit 400 (Bad Request);a – tiger Jan 23 '17 at 19:38
  • The 400 response is coming from your API, I'm talking about the outgoing request that gets sent to the API before you get the 400 in response – Alex Jan 23 '17 at 19:40
  • What I'm thinking is that you need to convert your JSON object into a JSON string, with JSON.Stringify(). I think the object is malformed in the request – Alex Jan 23 '17 at 19:44
  • im sorry i just understand what you mean. Yes, object get posted correctly. I chek this with console.log(LimitType..) which i put on top my method 'ChangeLimit' – tiger Jan 23 '17 at 19:47
  • i am also try with JSON.Stringify() and i also get those error – tiger Jan 23 '17 at 19:49

2 Answers2

0

Seeing this question and answer a 400 error indicates that your json is malformed.

From your code snippets, it seems that the line limitService.updateGameLimit(request); should provide the JSON, yet it is not included in the code snipets. Once you have the output of that method, you can run it through JsonLint to check the syntax. Then repairs can be made from there.

From your typescript client it seems that this is supplying invalid json. While I am not totally versed in typescript this certainly has some malformed JSON even if there are implied quotes. At the very least there should be double quotes around firstLimit, secondLimit, and key.

Community
  • 1
  • 1
Pete B.
  • 3,188
  • 6
  • 25
  • 38
0

It's because your json is not being formed properly. And there are multiple reasons for that

  1. Your Keys are supposed to be strings, and wrapped in quotes. eg: use "type" instead of type.
  2. You have a comma at the end of the line

    status: secondLimit.status,
    

    Remove that comma.

After you are done with it, validate a sample output on jsonlint.com or a similar service. It will help you figure out errors.

Gaurav Arya
  • 146
  • 3