Code Monkey home page Code Monkey logo

Comments (4)

gruns avatar gruns commented on August 25, 2024

To set individual query string parameters, as in your example, use the args or query_params parameter of add().

>>> from furl import furl
>>> f = furl('http://www.a.de/?a=b')
>>> f.add(args={'x':'z'})
>>> f.url
'http://www.a.de/?a=b&x=z'

args is also the first parameter of add(), and thus can be optionally omitted.

>>> f = furl('http://www.a.de/?a=b')
>>> f.add({'x':'z'})
>>> f.url
'http://www.a.de/?a=b&x=z'

Or, all in one line

>>> furl('http://www.a.de/?a=b').add({'x':'z'}).url
'http://www.a.de/?a=b&x=z'

Does that answer your question?

from furl.

sbi avatar sbi commented on August 25, 2024

I see I didn't specify the problem well enough, sorry! I'm looking for a way to replace parameters that might or might not be already present in the query string. Here's a better example:

>>> furl('http://www.a.de/?page=1').add({'page': '2'}).url
'http://www.a.de/?page=1&page=2
# I actually would like to get 'http://www.a.de/?page=2'

I could use set(), but that removes any other unrelated parameters that are in the query string:

>>> furl('http://www.a.de/?search=foo&page=1').set({'page': '2'}).url
'http://www.a.de/?page=2
# I actually would like to get 'http://www.a.de/?search=foo&page=2'

from furl.

gruns avatar gruns commented on August 25, 2024

The set() method of furl objects sets entire URL components, like the query, in the absolute sense. That is, set() replaces existing URL components with those provided.

To set individual query parameters with a dictionary-like object, call set() on the Query object instead of the parent furl object.

>>> f = furl('http://www.a.de/?search=foo&page=1')
>>> f.query.set({'page': 2})
>>> f.url
'http://www.a.de/?search=foo&page=2'

To accomplish the aforementioned in one line, combine remove() and add().

>>> from furl import furl
>>> furl('http://www.a.de/?search=foo&page=1').remove(['page']).add({'page': 2}).url
'http://www.a.de/?search=foo&page=2'

Do either of the above satisfy your needs?

from furl.

sbi avatar sbi commented on August 25, 2024

Yes, thank you :-)

from furl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.