asked    Susie     2018-10-22       ios       78 view        1 Answer

[SOLVED] Alamofire - Bad request

I am afraid I cannot share the API url. But I have checked on Postman, it works. It is a POST request and following is the response :

{
    "user_key": "b0aebdedb15e2beaaf479ca3c4f8227e8e970681"
}  

Postman screenshot :

enter image description here

In code, this is the request I am making using Alamofire :

Alamofire.request("some url", method: .post, parameters: params, encoding: URLEncoding.httpBody, headers: ["Content-Type":"application/json"])
            .responseObject { (response: DataResponse<User>) in

                let userResponse = response.result.value
                print("")
    }

But userResponse comes to be nil. This is the User object :

import ObjectMapper

class User: Mappable {

    var authToken: String?

     required init?(map: Map) {

    }

    func mapping(map: Map) {
        self.authToken <- map["user_key"]
    }


}

I am using ObjectMapper here.

Assuming the it is not a JSON object, I tried to handle it as a String :

 Alamofire.request("some url", method: .post, parameters: params, encoding: URLEncoding.httpBody, headers: ["Content-Type":"application/json"])
            .responseString { (response: DataResponse<String>) in
                let dataUser = response.data as! Any
                let dataUser1 = response.data as! AnyObject
                let error = response.error
                let code = response.response?.statusCode
                print(response.value)
                print(response.result.value)
        }  

Still, nothing. I am not able to retrieve the value. The status code however comes out to be 400 (bad request). What exactly is it ? JSON object or String ? How do I fix this ?

  1 Answer  

        answered    Novia     2018-10-22      

Just try to replace

this URLEncoding.httpBody to JSONEncoding.default

JSONEncoding.default

Uses JSONSerialization to create a JSON representation of the parameters object, which is set as the body of the request. The Content-Type HTTP header field of an encoded request is set to application/json.

Alamofire reference





Your Answer





 2018-10-22         Norman

Month year picker in angular4 validation error

I am New in Angular4. I want a Month and year picker. I try this<dp-date-picker #searchFromPicker mode="month" theme="dp-material" onkeydown="return false" (onChange)="onChangeSearchFrom($event)"></dp-date-picker><button class="glyphicon glyphicon-calendar" style="padding-top:5px; padding-bottom:5px;" (click)="openSearchFrom()" [disabled]="!editable"></button>.But validation such as disable,min,max etc is not working in this example.Refer any better option if available other than this. Please help me. Try mater...
 angular                     1 answers                     56 view
 2018-10-22         Allen

How can i integrate chat feature in my Angular4 application?

I want to integrate chatting feature in Angular4 application with text message, smilies, attachment sharing, group chat etc.Chatting between the users of my application. Users list is available from my database. Want to do chat among them.Is there any open source frameworks for this?Please assist You can achieve that by Integrating Web Sockets, Node & Angular WebSockets is a protocol that provides a bi-directional communication channel. This means that a browser and web server can maintain real-time comms, sending messages back and forth while the connection is open...
 angular                     1 answers                     56 view
 2018-10-22         Freda

Parsing API Response (object) from component to another component in Angular 4

I want to parse API response to another componentThis is code from 'detail_item.component.ts', this code sent data to route "bookitem" in 'book_item.component.ts'onSubmit(){ this.appService.addItem(this.item).subscribe(item => { if(item.status ==200){ this.router.navigate(['/bookitem'], {queryParams: {data: item.data}}); } })}this is code from 'book_item.component.ts'ngOnInit() { this.routeActive.queryParams.filter(params => params.data).subscribe(params => { this.book_item = params.data; });}when I console.log(this.book_item), wh...
 angular                     2 answers                     56 view