Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
Swift HTTP Request Issue (Not sending HOST Header)

I have a simple rest endpoint built on PHP that works on postman and browsers.

https://someserver.com/api/endpoint.php?name=hello

But the code fails to execute when invoked from Swift. Upon Investigation, I found out that Postman and Browser sends the Host Header whereas the Swift URLRequest doesn't. When you don't send the HOST Header, the php server refuses to accept the incoming request and I believe the request is rejected at the web server level. I tried using the

request.addValue("127.0.0.1", "Host")

but it still didn't work. Any help is appreciated.

Full Code (Swift 4)

let url = urlInput.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
        print("URL is: " + url)
        var request = URLRequest(url: URL(string: url)!)
        request.httpMethod = "GET"
            request.addValue(<#T##value: String##String#>, forHTTPHeaderField: <#T##String#>)
        let session = URLSession.shared
        let task = session.dataTask(with: request, completionHandler: { data, response, error -> Void in
            if let httpResponse = response as? HTTPURLResponse {
                
                let respStr = String(data: data!, encoding: String.Encoding.utf8) as String?
                print ("Response is \(respStr ?? "")")
                print("statusCode: \(httpResponse.statusCode)")
                
                let data: Data? = respStr?.data(using: .utf8)
}
})

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Swift is ignoring the fact that you set the header, as it typically should. See the docs about Swift headers and why it's ignoring your Host header.

The URL Loading System handles various aspects of the HTTP protocol 
for you (HTTP 1.1 persistent connections, proxies, authentication, 
and so on). As part of this support, the URL Loading System takes 
responsibility for certain HTTP headers:

Content-Length

Authorization

Connection

Host

Proxy-Authenticate

Proxy-Authorization

WWW-Authenticate

If you set a value for one of these reserved headers, the system may 
ignore the value you set, or overwrite it with its own value, or 
simply not send it. Moreover, the exact behavior may change over 
time. To avoid confusing problems like this, do not set these headers 
directly.

The documentation is here:

https://developer.apple.com/documentation/foundation/nsurlrequest#1776617

I have seen this before with Java - there is a workaround in Java depending on the library you are using, but I don't know about Swift.

over 4 years ago · Santiago Trujillo Relatório

0

I figured out that the issue happens only when you hit a php server. The php web server looks for some header and cant find it when the call is made from Swift. I even copied the working code from Postman and results were the same.

The solution I went with was to create a proxy server using firebase cloud function. Swift code would hit a firebase cloud function which talks to php and sends the response to swift.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda