Skip to content
This repository has been archived by the owner on Dec 2, 2022. It is now read-only.

Unknown error (404) when sending message with question mark at the end #113

Closed
ghost opened this issue Jul 20, 2017 · 9 comments
Closed
Labels

Comments

@ghost
Copy link

ghost commented Jul 20, 2017

When trying to send message (bot.webApi?.sendMessage()) with text that has question mark (?) I get failure with unknownError type. When I removed this question mark my message got through. I think there is something wrong with escaping this sign from request url and it's messing with url query.

@ghost ghost changed the title Unknown error (404) when sending posting message with question mark at the end Unknown error (404) when sending message with question mark at the end Jul 20, 2017
@pvzig
Copy link
Owner

pvzig commented Jul 21, 2017

Hi Łukasz,

What platform are you seeing this on?

Thanks,
Peter

@pvzig pvzig added the bug label Jul 21, 2017
@ghost
Copy link
Author

ghost commented Jul 24, 2017

I am using my local machine (Macbook) and compiling project with Swift 3.
The only library in my package.swift is SlackKit (majorVersion 4).
And here's the code I am trying to run:

import Foundation
import SlackKit

class Bot {
    let bot: SlackKit
    
    init(token: String) {
        bot = SlackKit()
        bot.addRTMBotWithAPIToken(token)
        bot.addWebAPIAccessWithToken(token)
        bot.webAPI?.sendMessage(channel: "my-channel-name", text: "wat?", asUser: true, success: { _, _ in
            print("success")
        }, failure: {error in
            print("fail with error: \(error)")
        })
    }
}

let slackbot = Bot(token: "xoxb-proper-token")
RunLoop.main.run()

When I change text to "wat" (without the question mark) it goes smoothly and it's posted on my channel.

@johntmcintosh
Copy link
Contributor

I ran into this same issue this week as well, and traced it down to the presence of a question mark in a query parameter for a URL that I was including as an attachment on a message. I reached out to slack's support team, who said the following:

This is indeed due to the question mark: we recently released a change that started enforcing URL encoding of parameters, and while we expected users to encode those parameters, we understand that this broke some existing integrations, our apologies for the inconvenience.

To fix this error, you will need to either URL encode all your parameters values, or switch to a POST request and pass these in the request's body.

@pvzig
Copy link
Owner

pvzig commented Aug 3, 2017

I was under the impression that using URLQueryItem would encode parameters correctly but maybe that's not the case...

https://github.com/SlackKit/SKWebAPI/blob/master/Sources/NetworkInterface.swift#L45

@johntmcintosh
Copy link
Contributor

johntmcintosh commented Aug 3, 2017

@pvzig Here's what I'm seeing as the output of the generated request:

https://slack.com/api/chat.postMessage?
attachments=%5B%7B%22thumb_url%22:%22https:%5C/%5C/s3.amazonaws.com%5C/ftb-web%5C/development%5C/reps%5C/avatars%5C/000%5C/000%5C/796%5C/original%5C/FullSizeRender.jpg?1433290383%22%7D%5D&as_user=false&channel=G4X7VCFN0&token=<REDACTED>&text=Test%20message

Looks like everything is being encoded except the question mark. I'm not immediately sure what the right approach is (as I've typically just used URLQueryItem for other things I've done as well.

(Also, note this output is from the last 3.x release of the library. I'm in the process of updating to 4.x now.)

@johntmcintosh
Copy link
Contributor

FYI - I'm digging into this a bit this morning and will plan on getting a PR together for you to take a look at.

@pvzig
Copy link
Owner

pvzig commented Aug 4, 2017

Thanks John, appreciate it!

@johntmcintosh
Copy link
Contributor

@pvzig I was able to find an open radar report discussing this same thing related to + signs, where an apple employee describes that Apple chose a particular interpretation of what needs to be encoded, which might differ from what other APIs expect. They provide a suggest of the correct way to manually apply the additional necessary encoding. I've just created a PR for SKWebAPI (pvzig/SKWebAPI#7) which adds this custom encoding for + and ?.

@pvzig
Copy link
Owner

pvzig commented Aug 7, 2017

This should be fixed with the 4.0.7 release of SKWebAPI. Rebuilding SlackKit should get you the latest. Let me know if you continue having issues after updating.

@pvzig pvzig closed this as completed Aug 7, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants