# How to escape boolean operators in HTTP.escapeuri()?

**URL:** https://discourse.julialang.org/t/how-to-escape-boolean-operators-in-http-escapeuri/46733
**Category:** General Usage
**Tags:** question, http
**Created:** [September 16, 2020, 10:30pm UTC](https://discourse.julialang.org/t/how-to-escape-boolean-operators-in-http-escapeuri/46733 "2020-09-16T22:30:51Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![mkarikom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkarikom/32/7096_2.png) [@mkarikom](https://discourse.julialang.org/u/mkarikom)
#### Post date: [September 16, 2020, 10:30pm UTC](https://discourse.julialang.org/t/how-to-escape-boolean-operators-in-http-escapeuri/46733/1 "2020-09-16T22:30:51Z")

</div>

I need to escape a string that has characters defined in the [apache Lucene implementation](https://lucene.apache.org/core/4_10_4/queryparser/org/apache/lucene/queryparser/classic/package-summary.html#Boolean_operators)

Ex. 1 (the “+” asserts that the identifier `intNum` is included somewhere in the doc)

```julia
+intNum%20NOT%20natNum # all non-positive integers

```

MWE, the “+” is getting url-escaped and I don’t want this

```julia
str = HTTP.escapeuri("+intNum NOT natNum")
@test str == "+intNum%20NOT%20natNum"
false

println(str)
"%2B%intNum%20NOT%20natNum"

```

attempting to manually escape the escaping didn’t work:

```julia
query = HTTP.escapeuri("\\+intNum NOT natNum")
"%5C%intNum%20NOT%20natNum"

query = HTTP.escapeuri("\+intNum NOT natNum")
ERROR: syntax: invalid escape sequence

```

Is there some other package I should be using for this?

---

<div class="post-metadata">

### Author: ![mkarikom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkarikom/32/7096_2.png) [@mkarikom](https://discourse.julialang.org/u/mkarikom)
#### Post date: [September 17, 2020, 3:44am UTC](https://discourse.julialang.org/t/how-to-escape-boolean-operators-in-http-escapeuri/46733/2 "2020-09-17T03:44:40Z")

</div>

This module overloads `HTTP.issafe()`  
I just [copied everything from line 300-321](https://github.com/JuliaWeb/HTTP.jl/blob/b127f4d17c42160ec0b7b28d46349bb2e509991e/src/URIs.jl#L300) and added a line to make ‘+’ not get escaped):

```julia
using Pkg;Pkg.activate("./")

module LuceneSearch
  import HTTP.IOExtras

  export issafe,escapeuri

  @inline issafe(c::Char) = c == '-' ||
                            c == '.' ||
                            c == '_' ||
                            c == '+' || # added '+' to the list of characters to ignore
                            (isascii(c) && (isletter(c) || isnumeric(c)))

  utf8_chars(str::AbstractString) = (Char(c) for c in IOExtras.bytes(str))

  "percent-encode a string, dict, or pair for a uri"
  function escapeuri end

  escapeuri(c::Char) = string('%', uppercase(string(Int(c), base=16, pad=2)))
  escapeuri(str::AbstractString, safe::Function=issafe) =
    join(safe(c) ? c : escapeuri(c) for c in utf8_chars(str))

  escapeuri(bytes::Vector{UInt8}) = bytes
  escapeuri(v::Number) = escapeuri(string(v))
  escapeuri(v::Symbol) = escapeuri(string(v))

  escapeuri(key, value) = string(escapeuri(key), "=", escapeuri(value))
  escapeuri(key, values::Vector) = escapeuri(key => v for v in values)
  escapeuri(query) = join((escapeuri(k, v) for (k,v) in query), "&")
  escapeuri(nt::NamedTuple) = escapeuri(pairs(nt))
end

```

Seems to work fine:

```julia
using Test,HTTP,.LuceneSearch

@testset "uri escaping" begin
  @test HTTP.escapeuri("i+am sam.") == "i%2Bam%20sam."
  @test LuceneSearch.escapeuri("i+am sam.") == "i+am%20sam."
end

Test Summary: | Pass Total
uri escaping | 2 2
Test.DefaultTestSet("uri escaping", Any[], 2, false)

```
