Skip to content

Status code 405 returned for the first POST call of the app even when POST is an allowed method on the API. #1533

Closed
@shwetachitlangia

Description

@shwetachitlangia

Moya version ->10.0.1

I am made a LoginAPI class with following method implementation.

public var method: Moya.Method {
        return .post
    }

The Login Service allows only POST HTTP method.
The issue is even after specifying Moya method as POST, the service response returns 405 status code in service call first time. If I retry the same call, status code 200 is returned.

Logs with 405 status code:
Request: https://myURL/login
Moya_Logger: [07/01/2018 21:08:54] Request Headers: ["Content-Type": "application/json"]
Moya_Logger: [07/01/2018 21:08:54] HTTP Request Method: POST
Moya_Logger: [07/01/2018 21:08:54] Request Body: {"password”:”password”,”email”:”abc@del.com"}
Moya_Logger: [07/01/2018 21:08:54] Response: <NSHTTPURLResponse: 0x608000038ac0> { URL: Request: https://myURL/login } { Status Code: 405, Headers {
Allow = (
POST
);
Date = (
"Sun, 07 Jan 2018 15:35:42 GMT"
);
"Transfer-Encoding" = (
Identity
);
} }

Logs with 200 Status code:
Request: https://myURL/login
Moya_Logger: [07/01/2018 21:12:35] Request Headers: ["Content-Type": "application/json"]
Moya_Logger: [07/01/2018 21:12:35] HTTP Request Method: POST
Moya_Logger: [07/01/2018 21:12:35] Request Body: {"password”:”password”,”email”:”abc@del.com"}
Moya_Logger: [07/01/2018 21:12:38] Response: <NSHTTPURLResponse: 0x60400043ab80> { URL: Request: https://myURL/login } { Status Code: 200, Headers {
"Content-Type" = (
"application/json;charset=UTF-8"
);
Date = (
"Sun, 07 Jan 2018 15:39:26 GMT"
);
"Transfer-Encoding" = (
Identity
);
} }

Points to note:

  1. This url works perfectly in Postman everytime with 200 status code with POST method.
  2. On simulator, this is the behavior first time I run the app (fresh launch of the app). On next run or on retrying , it works.
  3. Tested the api call by refactoring it to an Alamofire call, it works.

Could someone please help with what could be the possible cause for this?

Activity

zhongwuzw

zhongwuzw commented on Jan 7, 2018

@zhongwuzw
Member

Can you capture the packet when you request Post first time? Provide it can found the issue more specifically if you provide HTTP content.

LavareX

LavareX commented on Jan 8, 2018

@LavareX
Member

@shwetachitlangia Please fill in the appropriate title, help others quickly understand your problem. :]

changed the title [-]5[/-] [+]Status code 405 returned for the first POST call of the app eben when POST is an allowed method on the API.[/+] on Jan 8, 2018
changed the title [-]Status code 405 returned for the first POST call of the app eben when POST is an allowed method on the API.[/-] [+]Status code 405 returned for the first POST call of the app even when POST is an allowed method on the API.[/+] on Jan 8, 2018
shwetachitlangia

shwetachitlangia commented on Jan 8, 2018

@shwetachitlangia
Author

@zhongwuzw Sorry, I am unable to track packets using Wireshark. But i rewrote the same api call with Alamofire and tested with same conditions, it works.

The issue seems to be with the First POST operation of the app.
The first GET operations works just fine.

AndrewSB

AndrewSB commented on Jan 8, 2018

@AndrewSB
Member

@shwetachitlangia without more information it's going to be hard to help you debug your issue. This would be a massive bug in Moya if it is an issue with Moya, but (from the fact that hundreds, if not thousands of apps are in production with Moya) it's more likely an issue with your code than the library.

Do you think you can make a Playground/example project that shows the issue? Without wireshark logs that would be most helpful for us to get your bug sorted out.

shwetachitlangia

shwetachitlangia commented on Jan 8, 2018

@shwetachitlangia
Author

Can this code help?
Doesn't seem like a code issue as we are calling the same API again and it works.
Cannot share an example project as we have secured services.

//-------------------------------------------------------------------------
// Login APIs - Login user
//-------------------------------------------------------------------------

import Moya

let loginProvider = NetworkUtility.getMoyaProvider(LoginAPI.self)

public enum LoginAPI {
    case loginUser([String: Any])
    case forgotPasswordOTP(String)
    case resetPassword([String: Any])
}

extension LoginAPI: TargetType {

    public var baseURL: URL { return URL(string: ConfigData.shared.getURL(forKey: DefaultsKeys.baseURL))! }

    public var path: String {
        switch self {
        case .loginUser:
            return ConfigData.shared.getURL(forKey: DefaultsKeys.login)
        case .resetPassword:
            return ConfigData.shared.getURL(forKey: DefaultsKeys.resetPassword)
        case .forgotPasswordOTP:
            return ConfigData.shared.getURL(forKey: DefaultsKeys.forgotPasswordOTP)
        }
    }

    public var method: Moya.Method {
        switch self {
        case .loginUser:
            return .post
        case .resetPassword, .forgotPasswordOTP:
            return .put
        }
    }

    public var task: Task {
        switch self {

        case .loginUser(let bodyParameters),
              //UserLogin dictionaryRepresentation is used for this
             .resetPassword(let bodyParameters):
            return .requestCompositeParameters(bodyParameters: bodyParameters,
                                               bodyEncoding: JSONEncoding.default,
                                               urlParameters: [:])
        case .forgotPasswordOTP(let email):
            return .requestCompositeParameters(bodyParameters: [User.Keys.email: email],
                                               bodyEncoding: JSONEncoding.default,
                                               urlParameters: [:])
        }
    }

    public var headers: [String: String]? {
        return NetworkUtility.getHeaders()
    }

    public var sampleData: Data {
        var data: Data! = nil
        switch self {
        case .loginUser:
            data = NetworkUtility.loadJSON(jsonName: "3.33.4Success")
        case .resetPassword:
            data = NetworkUtility.loadJSON(jsonName: "ResetPassword")
        case .forgotPasswordOTP:
            data = NetworkUtility.loadJSON(jsonName: "VerifyUser")
        }
        return data
    }
}
LavareX

LavareX commented on Jan 9, 2018

@LavareX
Member

@shwetachitlangia The above code snippet provides insufficient information. Or you can request third-party public services (for example: https://httpbin.org), see the test will appear the same problem? In order to troubleshoot the problem.

karbhasin-zz

karbhasin-zz commented on Jan 15, 2018

@karbhasin-zz

@yangcaimu Facing exactly the same issue. Sending the following POST request -

Request: http://XXXXXX/YYYY/UUUIIUIIUIUIUIUIU
Moya_Logger: [15/01/2018 16:10:08] Request Headers: ["Content-Type": "application/json", "correlationId": "876578658769389283kjhgf"]
Moya_Logger: [15/01/2018 16:10:08] HTTP Request Method: POST
Moya_Logger: [15/01/2018 16:10:08] Request Body: {"password":"password","email":"shshs@sd.com"}..

Checked with server team, as per their logs, they are getting method type as "ST" instead of POST.

AS per Server Logs -
"o.s.web.servlet.DispatcherServlet - DispatcherServlet with name ‘dispatcherServlet’ processing ST request for [/YYYY/UUUIIUIIUIUIUIUIU]"

zhongwuzw

zhongwuzw commented on Jan 16, 2018

@zhongwuzw
Member

@karbhasin , strange behavior, maybe the only solution is to get requests raw packet from client-side and server-side, then compare them to find the issue.

aloco

aloco commented on Jan 17, 2018

@aloco
Contributor

Hi,

I am updating a project from Moya 7.0.0 (swift 2.x) to Moya 10.0.0, facing the same issue. @shwetachitlangia did you find any workarounds?

sunshinejr

sunshinejr commented on Jan 17, 2018

@sunshinejr
Member

This is really strange and unfortunately all of you guys can't share your project to help us investigate it, right?

In that case, will any of you guys (@aloco, @karbhasin, @shwetachitlangia) want to make a quick call with me and we could try debug it on the fly? Being able to reproduce it and access to the server logs would be awesome.

shwetachitlangia

shwetachitlangia commented on Jan 17, 2018

@shwetachitlangia
Author

@sunshinejr I can have a call with you. I can reproduce it. But don't have access to server logs.
My skype id is chitstcs.

SD10

SD10 commented on Jan 17, 2018

@SD10
Member

@shwetachitlangia Did this issue occur after a migration to 10.0.1? If so, what was the previous Moya version where this issue was not present?

Sent with GitHawk

30 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @LavareX@shwetachitlangia@AndrewSB@BasThomas@zhongwuzw

        Issue actions

          Status code 405 returned for the first POST call of the app even when POST is an allowed method on the API. · Issue #1533 · Moya/Moya