0

I am writing an iOS App in Swift 4.2. I am using Moya Networking.

I have to use below GET query for fetching data from server:

https://api.backendless.com/AABE5C4B-AA58-955C-FF16-27B64A185300/46FDFF59-CF95-B699-FFF5-83B681610700/data/quilt_detail?where=addedDate>1555515000 AND quitID='9FE17AA3-E182-6DFA-FF1D-52B33B2B8D00' AND ownerId='AC1CB90D-CF3E-7243-FF87-9E408D68E800'&props=sleepHours,addedDay,addedDate

My Code:

var task: Task {
        switch self{
        case .getTrends(let quiltID, let addedDate, let ownerId):
            return .requestParameters(parameters: ["where":"addedDate > \(addedDate) AND quitID='\(quiltID)' AND ownerId ='\(ownerId)'", "props":"sleepHours,addedDay,addedDate"], encoding: URLEncoding.default)
        }
    }

But issue it, while executing it makes below GET Query:

https://api.backendless.com/AABE5C4B-AA58-955C-FF16-27B64A185300/46FDFF59-CF95-B699-FFF5-83B681610700/data/quilt_detail?props=sleepHours%2CaddedDay%2CaddedDate&where=addedDate%20%3E%201556955782%20AND%20quitID%3D%276264A540-84F3-4D09-FF8D-00D4C94E9D00%27%20AND%20ownerId%20%3D%27DB64B524-8973-9357-FF67-3C69B6CD1C00%27

Instead of:

addedDate>1555515000 AND quitID='9FE17AA3-E182-6DFA-FF1D-52B33B2B8D00' AND ownerId='AC1CB90D-CF3E-7243-FF87-9E408D68E800'

its making:

addedDate%20%3E%201556955782%20AND%20quitID%3D%276264A540-84F3-4D09-FF8D-00D4C94E9D00%27%20AND%20ownerId%20%3D%27DB64B524-8973-9357-FF67-3C69B6CD1C00%27

How to make a GET query with spaces?

Atif Shabeer
  • 167
  • 3
  • 16

2 Answers2

0

URLs string never has spaces Check Here

0

Your query does contain the spaces. They've just been percent-encoded or also known as URL-encoded. This is a mechanism to include characters in a URL that would otherwise not be legal or that would conflict with the URL structure. Learn more on Wikipedia's percent-encoding article.

A simple example would be a GET param value that contains a &. You have to encode it otherwise the value after it would start a new param.

The server receiving the request will see the original data that you sent. You can verify this in your browser's console with the decodeURIComponent:

decodeURIComponent('addedDate%20%3E%201556955782%20AND%20quitID%3D%276264A540-84F3-4D09-FF8D-00D4C94E9D00%27%20AND%20ownerId%20%3D%27DB64B524-8973-9357-FF67-3C69B6CD1C00%27')

This returns:

addedDate > 1556955782 AND quitID='6264A540-84F3-4D09-FF8D-00D4C94E9D00' AND ownerId ='DB64B524-8973-9357-FF67-3C69B6CD1C00'
fphilipe
  • 9,739
  • 1
  • 40
  • 52